package com.itheima.eureka_consumer.controller;


import com.itheima.eureka_consumer.config.RestTemplate11;
import com.itheima.eureka_consumer.domain.Googs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.List;
@RestController
@RequestMapping("/order")
public class GetGoogsController {
    @Autowired
    private DiscoveryClient discoveryClient;
    @Autowired
    private RestTemplate restTemplate11;
    @GetMapping("/xxx/{id}")
    public Googs findOne(@PathVariable("id") Integer id){
        System.out.println("....");
        //通过server服务器的重命名 获取值
        List<ServiceInstance> instances = discoveryClient.getInstances("EUREKA-PROVIDER");

//判断集合是否有数据
        if(instances == null || instances.size() == 0){
            //集合没有数据
            return null;
        }

        ServiceInstance instance = instances.get(0);
        String host = instance.getHost();//获取ip
        int port = instance.getPort();//获取端口
        String url="http://"+host+":"+port+"/text/xxx/"+id;
        Googs googs = restTemplate11.getForObject(url, Googs.class);
        return  googs ;

    }


}
